import {
  judgePrivate,
  startConference,
  singleJoinMeeting
} from '@/api/conferences'

export default {
  data () {
    return {
      // 九位短会议号
      meetingNumber: '',
      // 当前会议id
      meetingId: '',
      // 当前会议状态
      meetingStatus: 0,
      // 密码框弹窗
      joinMeetingDialogVisible: false,
      // 按钮加载标志
      joinLoading: false
    }
  },
  computed: {
    reallyNumber () {
      return this.meetingNumber ? this.meetingNumber.replace(/\s/g, '') : ''
    }
  },
  methods: {
    // 设置输入框内容
    setNum () {
      this.meetingNumber = this.meetingNumber
        .replace(/\s/g, '')
        .replace(/[^\d]/g, '')
        .replace(/(\d{3})(?=\d)/g, '$1 ')
    },
    async handleJoin () {
      console.log('join')
      if (!this.reallyNumber) {
        this.$message.warning('请输入会议号')
        return
      }
      this.joinLoading = true
      try {
        const { data } = await judgePrivate(this.reallyNumber)
        const conferenceInfo = data.conferenceBaseVO
        this.meetingId = conferenceInfo.cid
        this.meetingStatus = conferenceInfo.status
        if (this.meetingStatus === 4) {
          this.joinLoading = false
          this.$message.info('会议已取消')
        } else if (this.meetingStatus === 5) {
          this.joinLoading = false
          this.$message.info('会议已结束')
        } else if (this.meetingStatus === 1) {
          // 判断是否是会议成员
          if (data.isConferenceMember) {
            // 判断是否是主持人
            if (data.isFounder) {
              this.handleStartConference(conferenceInfo.cid)
            } else {
              this.joinLoading = false
              this.$message.info('会议未开始')
            }
          } else {
            // 判断是否需要密码
            if (conferenceInfo.requiredPassword) {
              this.joinMeetingDialogVisible = true
            } else {
              this.handleSingleJoinMeeting()
            }
          }
        } else if (this.meetingStatus === 2) {
          // 判断是否是会议成员
          if (data.isConferenceMember) {
            this.joinLoading = false
            this.openMeetingRoom(this.meetingId)
          } else {
            // 判断是否需要密码
            if (conferenceInfo.requiredPassword) {
              this.joinLoading = false
              this.joinMeetingDialogVisible = true
            } else {
              this.handleSingleJoinMeeting()
            }
          }
        }
      } catch {
        this.joinLoading = false
      }
    },
    // 开始一个会议
    async handleStartConference () {
      try {
        await startConference(this.meetingId)
        this.joinLoading = false
        this.openMeetingRoom(this.meetingId)
      } catch {
        this.joinLoading = false
      }
    },
    // 无需密码加入会议
    async handleSingleJoinMeeting () {
      try {
        await singleJoinMeeting(this.meetingId, '')
        this.joinLoading = false
        this.handleSingleJoin()
      } catch {
        this.joinLoading = false
      }
    },
    // 输入密码后的事件
    handleSingleJoin () {
      this.joinMeetingDialogVisible = false
      if (this.meetingStatus === 1) {
        this.$message.info('会议未开始')
      } else if (this.meetingStatus === 2) {
        this.openMeetingRoom(this.meetingId)
      } else {
        this.$message.error('会议信息出错，请稍后再试')
      }
    }
  }
}
